// pages/business-hall/business-hall.js
import Http from '../../../utils/http.js';
import { formatDistance } from '../../../utils/util.js';

const http = new Http();
const app = getApp();
Page({
    /**
     * 页面的初始数据
     */
    data: {
        navbtn: '', //navbtn  01--操作记录进入-显示顶部导航
        review: '', //审核字段
        imageArr: [],
        isIphone: false,
        id: '',
        uid: '',
        address: '',
        city: '',
        country: '',
        contactor: '',
        createtime: '',
        realnapicurl: '', //发布人头像
        inputphone: '', //发布人手机号
        inputrealname: '',
        lat: '',
        lng: '',
        operator: '',
        operatorpicurl: '', //运营商logo
        phone: '',
        backup: '',
        distance: '',
        //操作弹框
        show: false,
        actions: [],
        //评论相关
        toView: '', //跳转留言id
        commentTop: 101,
        remarkEdit: false,
        remarkValue: '', //评论/回复内容
        tocommentid: '', //被回复的评论id,回复时传
        placeHoldComment: '欢迎留言讨论', //评论/回复内容
        touid: '',
        toreplyid: '',
        privateflg: '00',
        createuid: '',
        commentList: [],
        commentcount: 0,
        // 点赞相关
        // 默认不点赞
        praiseflg: '00',
        praisecnt: 0,
    },
    // 查看他人主页
    jumpOthersPage(e) {
        wx.navigateTo({
            url: `/functionPage/pages/othersHomePage/othersHomePage?uid=${e.currentTarget.dataset.uid}`,
        });
    },
    onLoad: function (options) {
        let _that = this;
        wx.getSystemInfo({
            success: function (res) {
                let isIpx;
                //model中包含着设备信息
                var model = res.model;
                if (model.search('iPhone') != -1) {
                    isIpx = true;
                } else {
                    isIpx = false;
                }
                _that.setData({
                    isIphone: isIpx,
                });
            },
        });
        _that.setData({
            id: options.id,
            review: options.review || '',
            navbtn: options.navbtn, //navbtn  01--操作记录进入
        });
        _that.getBusinesshallDetails();
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {},
    onReady() {
        this.footComment = this.selectComponent('#foot-comment');
    },
    //导航
    navigateBtn(e) {
        let _that = this;
        wx.openLocation({
            //​使用微信内置地图查看位置。
            latitude: _that.data.lat,
            longitude: _that.data.lng, //要去的经度-地址
            name: _that.data.operator,
            address: _that.data.address,
        });
    },
    //显示弹出层
    editBtn(e) {
        // 导航栏置灰
        wx.setNavigationBarColor({
            frontColor: '#000000',
            backgroundColor: '#484848',
            animation: {
                duration: 300,
                timingFunc: 'easeIn',
            },
        });
        if (e.currentTarget.dataset.comment != '01') {
            this.setData({
                show: true,
                actions: [
                    {
                        name: '删除',
                        value: 'remark-delete',
                        color: '#FF455E',
                    },
                ],
                remarkId: e.detail.id,
            });
        } else {
            this.setData({
                show: true,
                actions: [
                    {
                        name: '编辑',
                        value: 'content-edit',
                        color: '#333333',
                    },
                    {
                        name: '删除',
                        value: 'content-delete',
                        color: '#FF455E',
                    },
                ],
            });
        }
    },
    //取消
    onCancel(e) {
        // 导航栏置白
        wx.setNavigationBarColor({
            frontColor: '#000000',
            backgroundColor: '#ffffff',
            animation: {
                duration: 300,
                timingFunc: 'easeIn',
            },
        });
        this.setData({
            show: false,
        });
    },
    onDelect(e) {
        let _that = this;
        console.log(e.detail.name);
        if (e.detail.value == 'content-edit') {
            _that.onCancel();
            wx.navigateTo({
                url: `../../../releasePage/pages/addBusinessHall/addBusinessHall?id=${_that.data.id}`,
            });
            _that.setData({
                show: false,
            });
        } else if (e.detail.value == 'content-delete') {
            _that.onCancel();
            _that.setData({
                show: false,
            });
            wx.showModal({
                title: '提示',
                content: '确认要删除该项吗？',
                success: function (res) {
                    if (res.confirm) {
                        //文章内容删除
                        wx.request({
                            url:
                                app.globalData.rootPath +
                                `cjMap/deleteBusinesshall`,
                            method: 'GET',
                            header: {
                                'content-type':
                                    'application/x-www-form-urlencoded',
                            },
                            data: {
                                authId: app.globalData.authId,
                                id: _that.data.id,
                                lat: app.globalData.locationLat,
                                lng: app.globalData.locationLng,
                            },
                            success: function (res) {
                                wx.showToast({
                                    title: '删除成功',
                                    icon: 'success',
                                    duration: 2000,
                                });
                                let pages = getCurrentPages(); //当前页面栈
                                let prevPage = pages[pages.length - 2]; //上一页面
                                if (
                                    prevPage &&
                                    typeof prevPage.refresh == 'function'
                                ) {
                                    try {
                                        prevPage.refresh(['businesshall']);
                                    } catch (err) {
                                        try {
                                            prevPage.getOperationList();
                                        } catch (err) {
                                            console.log(err);
                                        }
                                    }
                                }
                                setTimeout(function () {
                                    wx.navigateBack({
                                        delta: 1, // 返回上一级页面。
                                    });
                                }, 2000);
                            },
                        });
                    } else {
                        _that.setData({
                            show: true,
                        });
                        console.log('点击取消回调');
                    }
                },
            });
        } else if (e.detail.value == 'remark-delete') {
            wx.showModal({
                title: '提示',
                content: '确认要删除该项吗？',
                success: function (res) {
                    if (res.confirm) {
                        _that.onCancel();
                        http.getRequest(
                            `cjMap/deleteComment?authId=${app.globalData.authId}&id=${_that.data.remarkId}`
                        ).then((res) => {
                            if (res.status == 0) {
                                wx.showToast({
                                    title: '删除成功',
                                    icon: 'none',
                                    duration: 1500,
                                });
                                _that.refreshComment();
                            }
                        });
                    } else {
                        _that.onCancel();
                        console.log('点击取消回调');
                    }
                },
            });
        }
    },
    //打电话
    callPhone(e) {
        if (!e.currentTarget.dataset.phone) return;
        wx.makePhoneCall({
            phoneNumber: e.currentTarget.dataset.phone, //仅为示例，并非真实的电话号码
        });
    },
    //获取营业厅内容
    getBusinesshallDetails(id) {
        let _that = this;
        http.getRequest(
            `cjMap/getBusinesshallDetails?authId=${app.globalData.authId}&id=${_that.data.id}`
        ).then((res) => {
            if (res.status == 0) {
                console.log(res);
                wx.setNavigationBarTitle({
                    title: '营业厅/' + res.data.city,
                });
                _that.setData({
                    uid: res.data.createuid,
                    createuid: res.data.createuid,
                    address: res.data.address,
                    contactor: res.data.contactor,
                    createtime: app.timeago(res.data.createtime),
                    inputphone: res.data.inputphone,
                    inputrealname: res.data.inputrealname,
                    lat: res.data.lat,
                    lng: res.data.lng,
                    city: res.data.city,
                    country: res.data.country,
                    operator: res.data.operator,
                    phone: res.data.phone,
                    imageArr: res.data.picurl
                        ? JSON.parse(res.data.picurl)
                        : [],
                    backup: res.data.backup,
                    distance: formatDistance(res.data.distance),
                    realnapicurl:
                        res.data.realnapicurl ||
                        'http://static.map8.com/sp/20200927/0_154453540424.jpg',
                    realname: res.data.inputrealname,
                    operatorpicurl: res.data.operatorpicurl,
                    commentList: res.data.commentList,
                    commentcount: res.data.commentcount,
                    praiseflg: res.data.praiseflg,
                    praisecnt: res.data.praisecnt,
                });
            }
        });
    },

    //评论
    jumpOrRemark(e) {
        if (this.data.commentTop > 100) {
            this.setData({
                toView: 'comment',
                commentTop: 0,
            });
        }
        if (e.detail.setplaceHold == '00') {
            this.footComment.articalRemark();
            this.onResetInputContent();
        }
    },

    // 重置输入框内容
    onResetInputContent() {
        this.setData({
            placeHoldComment: '欢迎留言讨论',
            tocommentid: '',
            touid: this.data.createuid,
            privateflg: '00',
        });
    },

    scrollTopFun(e) {},

    remarkInfo(e) {
        let _that = this;
        _that.footComment.remarkFocus('01');
        let name = e.detail.battleTag;
        _that.setData({
            tocommentid: e.detail.contentid || e.detail.tocommentid, //被回复的评论id,回复时传
            placeHoldComment: '@' + name, //评论/回复内容
            touid: e.detail.touid,
            toreplyid: e.detail.toreplyid,
            privateflg: e.detail.privateflg,
        });
    },
    remarkEditChange(e) {
        this.setData({
            remarkEdit: e.detail.remarkEdit, //被回复的评论id,回复时传
        });
    },
    refreshComment() {
        http.getRequest(
            `cjMap/getBusinesshallDetails?authId=${app.globalData.authId}&id=${this.data.id}`
        ).then((res) => {
            this.setData({
                commentList: res.data.commentList,
                commentcount: res.data.commentcount,
                praiseflg: res.data.praiseflg, //本人是否点赞
                praisecnt: res.data.praisecnt, //点赞数
            });
        });
    },
});
